package cool.tianyou.blue.bird.util;

import java.util.List;

import cn.hutool.cache.CacheUtil;
import cn.hutool.cache.impl.TimedCache;
import cool.tianyou.blue.bird.model.CommonMessage;

public class MessageCache {
    private TimedCache<String, CommonMessage> cache = CacheUtil.newTimedCache(30000);

    private static final MessageCache instance;
    static{
        instance = new MessageCache();
    }
    private MessageCache(){

    }

    public void add(CommonMessage message){
        cache.put(message.getMessageUuid(),message);
    }
    public void remove(String messageUuid){
        cache.remove(messageUuid);
    }
    public void messageAck(String messageUuid,Integer deviceId){
        CommonMessage commonMessage = cache.get(messageUuid);
        if(commonMessage==null){
            return;
        }
        List<Integer> pushedDeviceIds = commonMessage.getPushedDeviceIds();
        pushedDeviceIds.remove(deviceId);
        if(pushedDeviceIds.isEmpty()){
            cache.remove(messageUuid);
        }
    }
}
